import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import Kolor.;

/**
 * Created by IntelliJ IDEA.
 * User: roll
 * Date: 8/19/11
 * Time: 2:29 PM
 * To change this template use File | Settings | File Templates.
 */
public class KolorGui {
    private JTextPane codeTextPane;
    private JPanel panel1;
    private JButton connectButton;
    private JTextField UrlTextField;
    private JTextField SIDTextField;
    private JTextField userTextField;
    private JTextField passwdTextField;
    private JComboBox comboBox1;
    private JComboBox comboBox2;
    private JComboBox comboBox3;
    private JEditorPane editorPane1;
    private Kolor kolor;

    public static void main(String[] args) {
        JFrame frame = new JFrame("KolorGui");
        frame.setContentPane(new KolorGui().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public KolorGui() {
        kolor = new Kolor();
        UrlTextField.setText(kolor.Url);
        SIDTextField.setText(kolor.TNSName);
        userTextField.setText(kolor.UserID);
        passwdTextField.setText(kolor.Passwd);
        connectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Connect();
            }
        });
        /* comboBox1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GetScdObjectName();
            }
        });
       /*  comboBox1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                GetScdObjectName();

            }
        });
        comboBox1.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                //To change body of implemented methods use File | Settings | File Templates.
               // GetScdObjectName();
            }
        });*/
        comboBox1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                GetScdObjectName();

                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        comboBox2.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                GetThirdObjectName();
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        comboBox3.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                GetFrthObjectName();
            }
        });
    }

    private void GetFrthObjectName() {
        // System.out.println(comboBox3.getSelectedIndex());
        if (kolor.isConnected && comboBox1.getSelectedIndex() > 0 && comboBox2.getSelectedIndex() > 0 && comboBox3.getSelectedIndex() > 0) {
            StringBuffer sb = new StringBuffer();
            sb.append("Select distinct OBJECTVALUE4 ");
            sb.append(" from sysadm.pspcmprog ");
            sb.append(" where OBJECTVALUE1  = ? ");
            sb.append(" and OBJECTVALUE2  = ? ");
            sb.append(" and OBJECTVALUE3  = ? ");
            String s = sb.toString();
            int res = 0;
            try {
                PreparedStatement ps = kolor.connection.prepareStatement(s);
                ps.clearParameters();
                ps.setString(1, comboBox1.getSelectedItem().toString());
                ps.setString(2, comboBox2.getSelectedItem().toString());
                ps.setString(3, comboBox3.getSelectedItem().toString());
                ResultSet resultset = ps.executeQuery();
                for (int row_cnt = 1; resultset.next(); row_cnt++) {
                    res = row_cnt;
                }
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            // System.out.println(res);
            if (res < 2) {
                // System.out.println("dupa");
                StringBuffer sql = new StringBuffer();
                sql.append("Select PROGTXT ");
                sql.append(" from sysadm.pspcmprog ");
                sql.append(" where OBJECTVALUE1  = ? ");
                sql.append(" and OBJECTVALUE2  = ? ");
                sql.append(" and OBJECTVALUE3  = ? ");
                String str = sql.toString();
                try {
                    PreparedStatement ps = kolor.connection.prepareStatement(str);
                    ps.clearParameters();
                    ps.setString(1, comboBox1.getSelectedItem().toString());
                    ps.setString(2, comboBox2.getSelectedItem().toString());
                    ps.setString(3, comboBox3.getSelectedItem().toString());
                    ResultSet resultset = ps.executeQuery();
                    resultset.next();
                    editorPane1.setText(resultset.getString(1));
                } catch (SQLException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }
    }

    private void GetThirdObjectName() {
        if (kolor.isConnected && comboBox1.getSelectedIndex() > 0 && comboBox2.getSelectedIndex() > 0) {
            StringBuffer sb = new StringBuffer();
            sb.append("Select distinct OBJECTVALUE3 ");
            sb.append(" from sysadm.pspcmprog ");
            sb.append(" where OBJECTVALUE1  = ? ");
            sb.append(" and OBJECTVALUE2  = ? ");
            String s = sb.toString();
            comboBox3.removeAllItems();
            comboBox3.addItem("");
            try {
                PreparedStatement ps = kolor.connection.prepareStatement(s);
                ps.clearParameters();
                ps.setString(1, comboBox1.getSelectedItem().toString());
                ps.setString(2, comboBox2.getSelectedItem().toString());
                ResultSet resultset = ps.executeQuery();
                for (int row_cnt = 1; resultset.next(); row_cnt++) {
                    comboBox3.addItem(resultset.getString(1));
                }
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    private void GetScdObjectName() {
        if (kolor.isConnected && comboBox1.getSelectedIndex() != -1) {
            StringBuffer sb = new StringBuffer();
            sb.append("Select distinct OBJECTVALUE2 ");
            sb.append(" from sysadm.pspcmprog ");
            sb.append(" where OBJECTVALUE1  = ? ");
            String s = sb.toString();
            comboBox2.removeAllItems();
            comboBox2.addItem("");
            try {
                PreparedStatement ps = kolor.connection.prepareStatement(s);
                ps.clearParameters();
                ps.setString(1, comboBox1.getSelectedItem().toString());
                //System.out.println("'" + comboBox1.getSelectedItem().toString() + "'");
                ResultSet resultset = ps.executeQuery();
                for (int row_cnt = 1; resultset.next(); row_cnt++) {
                    comboBox2.addItem(resultset.getString(1));
                }
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }


    private void Connect() {
        if (!kolor.isConnected) {
            kolor.OpenConn();
            if (kolor.isConnected) {
                editorPane1.setText("connected");
                connectButton.setText("Disconnect");

                GetFirstObjectName();
            }
        } else {
            codeTextPane.setText("disconnected");

            kolor.CloseConn();
            connectButton.setText("Connect");
            comboBox1.removeAllItems();
        }
    }

    private void GetFirstObjectName() {
        String request = "SELECT DISTINCT OBJECTVALUE1 FROM SYSADM.PSPCMPROG ";
        comboBox1.addItem("");
        try {
            Statement ps = kolor.connection.createStatement();
            ResultSet resultset = ps.executeQuery("SELECT DISTINCT OBJECTVALUE1 FROM SYSADM.PSPCMPROG ");
            for (int row_cnt = 1; resultset.next(); row_cnt++) {
                comboBox1.addItem(resultset.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
